package LeetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class LeetCodeMain27 {
    public int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0];
            }
        });
        List<int[]> list=new ArrayList<>();
        for (int[] a:people) {
            list.add(a[1],a);
        }
        return list.toArray(new int[list.size()][2]);
    }
    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0]-o2[0];
            }
        });
        int count=0;
        for (int i = 0; i < points.length; i++) {
            int num=points[i][1];
            if (num==0){
                continue;
            }
            for (int j = i+1; j < points.length; j++) {
                if (num>=points[j][0]&&num<=points[j][1]){
                    points[j][0]=0;
                    points[j][1]=0;
                }
            }
            count++;
            points[i][0]=0;
            points[i][1]=0;
        }
        return count;
    }
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1]-o2[1];
            }
        });
        int count=1;
        int end=intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (end<=intervals[i][0]){
                end=intervals[i][1];
                count++;
            }
        }
        return intervals.length-count;
    }
}
